home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 015a / clockdoc.zip / RIGHTIME.TXT
Text File  |  1993-03-09  |  19KB  |  358 lines

  1. Note:
  2.  
  3.      The following paper was presented at PTTI '92 in McLean,
  4. Virginia on December 2, 1992.  The 24th Annual Precise Time and
  5. Time Interval Applications and Planning Meeting was held at the
  6. Ritz-Carlton Hotel at Tysons Corner.  The major emphasis was on
  7. systems that use time, time interval and frequency and that are
  8. not generally known or thought of as timed systems.  These include
  9. PTTI applications in major aerospace, military, commercial and
  10. telecommunications programs.  The Meeting is sponsored each year
  11. by the U.S. Naval Observatory, and is currently chaired by Sheila
  12. C. Faulkner.
  13.  
  14.  
  15.  
  16.  
  17.                       RighTime tm
  18.  
  19.          A Real Time Clock Correcting Program
  20.            For MS-DOS-Based Computer Systems
  21.  
  22.  
  23.                    G. Thomas Becker
  24.              Air System Technologies, Inc.
  25.               14232 Marsh Lane, Suite 339
  26.                Dallas, Texas  75234-3899
  27.                      214/402-9660
  28.                    214/869-1166 Fax
  29.                    214/869-2780 BBS
  30.  
  31.  
  32.                        Abstract
  33.  
  34.      A computer program is described which effectively eliminates
  35. the misgivings of the DOS system clock in PC/AT-class computers. 
  36. RighTime is a small, sophisticated memory-resident program that
  37. automatically corrects both the DOS system clock and the hardware
  38. "CMOS" real time clock (RTC) in real time.  RighTime learns what
  39. corrections are required without operator interaction beyond the
  40. occasional accurate time set.  Both warm (power on) and cool
  41. (power off) errors are corrected, usually yielding better than one
  42. part per million accuracy in the typical desktop computer with no
  43. additional hardware, and RighTime increases the system clock
  44. resolution from approximately 0.0549 second to 0.01 second. 
  45. Program tools are also available which allow visualization of
  46. RighTime's actions, verification of its performance, display of
  47. its history log, and which provide data for graphing of the system
  48. clock behavior.  The program has found application in a wide
  49. variety of industries, including astronomy, satellite tracking,
  50. communications, broadcasting, transportation, public utilities,
  51. manufacturing, medicine and the military.
  52.  
  53.  
  54.                      Introduction
  55.  
  56.      Most MS-DOS, PC-DOS and DRDOS users have long ago learned to
  57. live with, and generally regard as inadequate, the system time-of-
  58. day clock that is a standard component of these operating systems. 
  59. The typical DOS-based computer system clock exhibits inaccuracies
  60. that can range from a few seconds to several minutes per day, and
  61. the system can lose track of days at a time (on Friday, leave a
  62. DOS-based computer running at the office and go home for the
  63. weekend; when you return to the office Monday morning, the machine
  64. will very likely tell you it's Saturday).  These errors are the
  65. result of a combination of compromises at several steps in the IBM
  66. PC design process, circa 1980.  The consequences remain with users
  67. to this day.
  68.  
  69.      No autonomous RTC hardware was implemented in the original
  70. IBM PC announced in August, 1981.  Instead, software (partially in
  71. ROM-based BIOS firmware and partially in RAM-resident DOS) counted
  72. regular interrupts which were generated by an interval timer.  DOS
  73. converted the resulting "tick" count to conventional expressions
  74. of time of day when application software called for it, and, if
  75. during a request for the time DOS determined that midnight had
  76. passed, it incremented the system date.  Whenever the system was
  77. booted, the date and time was initialized to 1980/01/01 00:00:00;
  78. the user was expected to set both at each boot.
  79.  
  80.      With the introduction of the PC/AT in August, 1984, a
  81. hardware clock system - a Motorola MC146818 CMOS RTC and some
  82. associated circuitry - was included.  The part was powered by a
  83. battery when the system was not operating so it would maintain
  84. date and time continually.  Released at the same time, DOS Version
  85. 3 automatically read the CMOS RTC clock date and time at system
  86. boot and set its date and tick counts to match; aside from that
  87. initialization at boot, the hardware clock was not used.  When the
  88. DOS date or time was changed by the user, the change was not
  89. reflected in the CMOS RTC clock; it needed a separate setup
  90. utility program.  Later DOS versions set the CMOS RTC clock at the
  91. same time the DOS clock was set, but the CMOS RTC clock time was
  92. still read, and is still today, only at boot.
  93.  
  94.      No other changes have been made to the time-of-day clock
  95. mechanisms of the AT-class PC-compatible computer since 1984,
  96. except as have been applied to the CMOS RTC clock hardware itself. 
  97. (Dallas Semiconductor and Motorola now produce several compatible
  98. lithium-powered clock modules and Intel and other semiconductor
  99. producers have incorporated the RTC logic within large-scale
  100. integrated circuits that support the current microprocessors.) 
  101. The hardware manufacturers and software publishers moved on as if
  102. whatever few difficulties that existed were solved with the advent
  103. of the PC/AT.  The problems were neither solved nor few.
  104.  
  105.  
  106.              The Problems of The DOS Clock
  107.  
  108.      The interval timer that DOS uses to generate the regular
  109. tick that it counts is driven by an uncalibrated, unconditioned
  110. and usually non-adjustable 1.193 Mhz source (1.193 Mhz is one
  111. quarter of the original PC system clock frequency of 4.77 Mhz, and
  112. it is also one third of the NTSC television standard color burst
  113. frequency, 3.579 Mhz).  The interval timer is programmed to divide
  114. that frequency by 65,536 to produce an interrupt rate of
  115. approximately 18.206 ticks per second which determines the
  116. resolution of the standard DOS clock, approximately 54.925
  117. milliseconds.  DOS allows for the expression of time in 0.01
  118. second increments, but it cannot internally maintain that
  119. resolution nor represent decimal times exactly due to the tick
  120. resolution.
  121.  
  122.      DOS depends upon the cooperation of all software that runs
  123. on the machine to allow sufficient time for each interval timer
  124. tick interrupt to be counted.  This is not always possible, so
  125. some interrupts are missed and are not counted.  On a computer
  126. whose interrupt system is heavily loaded, the accumulation of lost
  127. interrupts makes the DOS clock appear to run slowly.
  128.  
  129.      The tick interrupt is normally intercepted by a routine in
  130. the ROM BIOS which increments a 32-bit tick count in RAM and
  131. determines if a value that represents approximately 24 hours has
  132. been exceeded (the tick duration makes 24 hours indeterminable
  133. exactly).  If so, the tick count is set to zero and a single bit
  134. flag, representing the passing of midnight, is set and provided to
  135. DOS when it next asks for the tick count.  These DOS requests only
  136. occur when the system is in need of the current time of day.  At
  137. that time, if the bit is set, DOS increments the date.  If more
  138. than 24 hours have elapsed between two requests for time of day,
  139. DOS is unable to recognize that more than one midnight has passed. 
  140. If the user displays the date after a system has been running, but
  141. idle for more than a day, DOS will indicate that the date is one
  142. day after the last day the machine was used.
  143.  
  144.      The interval timer has found itself reprogrammed by a number
  145. of applications, usually games that require dynamic video
  146. presentations.  In these programs, the interrupt that the interval
  147. timer yields is used to refresh the video screen at a high rate. 
  148. At the termination of the game, the DOS clock is rarely where it
  149. should be.  Some of the more responsible programs make an attempt
  150. at resetting the DOS clock to compensate for the game session
  151. duration, but others leave it in disarray.  If the game increased
  152. the tick rate and left the intercepting BIOS interrupt "hook" in
  153. place, the DOS clock will have advanced greatly; in the worst
  154. examples, the rate is left accelerated as well.
  155.  
  156.  
  157.              The Problems of The CMOS RTC
  158.  
  159.      The CMOS RTC clock resolves only to whole seconds.  When DOS
  160. reads it at boot, one second of ambiguity is set in the DOS clock
  161. even if the CMOS RTC clock is accurate.  Conversely, when the DOS
  162. clock is set, DOS transfers whole seconds to the CMOS RTC clock
  163. but makes no attempt to deal with the fractional part of the
  164. seconds, nor does it synchronize the seconds transition, so even
  165. if the DOS clock is set accurately, the CMOS RTC clock won't be.
  166.  
  167.      The CMOS RTC clock is usually paced by a 32.768 Khz quartz
  168. crystal which is loaded by a pair of capacitors.  The crystal is
  169. often an inexpensive watch-type device, characterized for
  170. operation at room temperature (typically 25 degrees C).  At
  171. temperatures either above or below the characterized temperature,
  172. the crystal's resonance change will slow the clock.  The loading
  173. capacitors are generally not temperature compensated and add to
  174. the detuning.  Except in the case of the modern CMOS RTC modules
  175. mentioned earlier, no provision is made to allow trimming the
  176. crystal frequency.  Dallas Semiconductor RTC modules are
  177. internally trimmed at the factory to +20 seconds per month at room
  178. temperature, anticipating an environment that will slow the clock;
  179. in use, the computer system internal temperature is widely
  180. varying, ranging from perhaps 10 degrees C when the system is not
  181. powered to 55 degrees C when it is.  Dallas Semiconductor's data
  182. indicates that the part can be expected to run as much as 2.5
  183. minutes per month slow at these temperatures.
  184.  
  185.      The CMOS RTC clock oscillator is powered by the system's +5
  186. volt supply when the machine is powered and by a lower voltage
  187. when it is on battery backup.  Typically, an additional six parts
  188. per million error can be expected when the clock is running on
  189. battery power.
  190.  
  191.      The CMOS RTC clock part provides an option that will
  192. automatically advance or retard the time at 0200 on the
  193. appropriate Sunday morning when Daylight Savings Time and Standard
  194. Time, respectively, start in the USA.  This feature is usually not
  195. used, since no mechanism to invoke it exists in DOS.  DOS does,
  196. nevertheless, include a bit in its internal time data structure
  197. that indicates whether this automatic time change feature is
  198. enabled or not (many documents incorrectly state that the bit
  199. indicates that the time is Daylight Savings Time).  The rule was
  200. changed in 1986 and many computers - even current models - contain
  201. parts that still adopt the pre-1986 rule.  Even if the feature is
  202. enabled, many machines will change on the wrong Sunday morning in
  203. April (the last rather than the first).  Dallas Semiconductor's
  204. DS1287 RTC module was introduced with the currently correct rule. 
  205. Motorola corrected its part with the announcement of the
  206. MCCS146818B1M RTC module in 1991.
  207.  
  208.  
  209.              The Solution to the Problems
  210.  
  211.      RighTime approaches the solution to these problems by making
  212. best use of the better qualities of each of the two clocks.  Two
  213. foremost qualities of the CMOS RTC are its relative stability and
  214. its autonomy.  The DOS clock has a high resolution interval timer
  215. available to it, although its availability is conditional, subject
  216. to loss of power and to software that commandeers it.
  217.  
  218.      With these resources, RighTime does four fundamental things:
  219.  
  220.      1-   RighTime slaves the DOS clock to the CMOS RTC,
  221.           frequently referring the DOS date, tick count and
  222.           interval timer count to the CMOS RTC date and time; 
  223.  
  224.      2-   RighTime operates the interval timer in a disciplined
  225.           mode that allows deriving a higher resolution than
  226.           standard while maintaining the standard tick rate and
  227.           count for compatibility;
  228.  
  229.      3-   RighTime maintains accuracy by regularly calculating
  230.           and applying corrections to the DOS clock and
  231.           adjustments to the CMOS RTC; and
  232.  
  233.      4-   RighTime intercepts and monitors time set commands to
  234.           learn and refine the CMOS RTC correction rate.
  235.  
  236.      RighTime is loaded each time DOS is booted, makes itself
  237. resident in system memory, and hooks a number of system
  238. interrupts.  (A hook is a logical tap in the execution chain of
  239. some specific event or class of events.)  For its time-keeping
  240. tasks, RighTime hooks an interrupt from the CMOS RTC, one from the
  241. interval timer, and several that convey the time set and read
  242. commands from an application program or the user to DOS.  Other
  243. interrupts are intercepted to disallow functions that could
  244. otherwise change resources that RighTime must exclusively control. 
  245. Since DOS is not ordinarily a multitasking operating system,
  246. several other interrupts are hooked to determine when system
  247. activities are momentarily idle.  These logical pauses are often
  248. very brief, but occur frequently and allow RighTime to do its
  249. manipulations as a transparent background function without
  250. noticeably affecting work that is ongoing in the foreground.
  251.  
  252.      The CMOS RTC is programmed to produce an interrupt once per
  253. second, immediately after its internal seconds update.  Every four
  254. seconds, RighTime reads the CMOS RTC date and time, calculates the
  255. equivalent DOS clock values, and sets the tick count and interval
  256. timer count and mode accordingly.  This regular update prevents
  257. the DOS clock from wandering far from the CMOS RTC time and
  258. corrects whatever ills might come from a game or other application
  259. that reprograms the interval timer.  Between these periodic
  260. updates, the DOS tick count is incremented by the BIOS interval
  261. timer interrupt handler - just as it is without RighTime - and is
  262. available to those programs that use the tick count directly.
  263.  
  264.      RighTime augments the routines that handle the conversions
  265. of tick count to time-of-day and time-of-day to tick count with
  266. its own routines.  These routines process the Get DOS Time and Set
  267. DOS Time functions, respectively, and provide resolution that far
  268. exceeds the 0.01 second resolution of the DOS clock data
  269. structure.  Any program that gets the time via the DOS calls will
  270. benefit from the additional resolution that RighTime provides.
  271.  
  272.      Although the CMOS RTC is relatively stable, its rate is
  273. rarely highly accurate.  Since, under RighTime, the DOS clock
  274. mimics the CMOS RTC, the DOS clock would exhibit the same rate
  275. error unless corrected.  RighTime handles this by incorporating a
  276. calculated correcting offset in each of the DOS clock updates. 
  277. The result is a deliberately increasing divergence of the two
  278. clocks.  Assuming that the corrected DOS clock rate is accurate,
  279. the CMOS RTC must be occasionally adjusted to match the DOS clock. 
  280. The CMOS RTC is advanced or retarded when the difference between
  281. the two clocks has reached one second; the DOS clock offset is
  282. adjusted accordingly.  If the DOS clock is set to the correct
  283. time, and the CMOS RTC rate correction is proper, the result is a
  284. DOS clock whose expression is within 0.01 second of the correct
  285. time and a CMOS RTC that is within one second of the correct time. 
  286. Actually, the calculated DOS clock offset is bipolar, so the
  287. CMOS RTC is never more than +/-0.5 second from the correct time.
  288.  
  289.      Since the CMOS RTC rate can vary with changes in temperature
  290. and voltage, the theoretically perfect correcting algorithm would
  291. account for both in appropriate complex terms.  In practice, two
  292. rates are maintained by RighTime, and suffice; one represents the
  293. system power-off state and the other represents power-on.  We
  294. refer to them as "cool" and "warm", though, since most users seem
  295. intuitively familiar with temperature effects.  RighTime applies a
  296. single calculated adjustment when it is loaded at system boot that
  297. corrects for CMOS RTC error that accumulated while the system was
  298. in the power-off state; if the program has not been running for
  299. more than 30 minutes, the cool correction rate is employed,
  300. otherwise the warm correction rate is applied.  With that
  301. exception, the warm correction rate is used in each DOS clock
  302. update.
  303.  
  304.      The CMOS RTC rate correction values are learned.  RighTime
  305. assumes that whenever the DOS clock is set, it is set accurately. 
  306. At the moment of set, then, the existing DOS clock error is a
  307. function of the CMOS RTC rate error, the current cool and warm
  308. correction rates, the time elapsed since the last accurate time
  309. set, and the ratio of warm and cool operation in that period.  The
  310. correction rates are refined with each time set, yielding
  311. decreasing error as they close on the "ideal" values.  These
  312. values, and others that are necessary for the process, are stored
  313. in a suitable place within the machine hardware or in a small
  314. dedicated file.
  315.  
  316.      For those users who want one less clock to change twice each
  317. year, RighTime offers support for the American Daylight/Standard
  318. time change feature of the CMOS RTC.  Since the change itself is
  319. handled by the hardware, the program does not need to be running
  320. when the change needs to occur.  The user is cautioned, though,
  321. that the change might not occur on the prescribed day in April due
  322. to an outdated hardware component.
  323.  
  324.      Although RighTime can usually be used effectively with the
  325. defaults, the program accepts a wealth of options and user-
  326. provided parameters that can detail the function to a specific
  327. machine environment, and a logical interface is included that
  328. allows the time setting functions to be automated.  A system can
  329. be easily designed that will continually adjust itself to a slowly
  330. changing operational environment as might be encountered with
  331. seasonal temperature changes at a remote data acquisition site.
  332.  
  333.  
  334.                       Conclusion
  335.  
  336.      RighTime is an effective software solution to the poor time-
  337. keeping performance of the PC/AT-compatible, DOS-based computer
  338. system.  The cesium beam and hydrogen maser clock industries will
  339. remain unchallenged by your computer, but it should be easy to
  340. achieve error rates of 0.5 second per week or better.  We
  341. regularly hear reports of one tenth of that error rate from stable
  342. systems, but these represent the best that can be expected in the
  343. current form of the program.  The current version (v2.5) requires
  344. only 6.5K of RAM, and the program can be loaded into high memory. 
  345.  
  346.      RighTime is a commercial software product that is currently
  347. distributed via shareware.  An evaluation copy is available
  348. electronically on the Air System Technologies BBS, 214/869-2780
  349. (1200-14400 bps, 8N1), free of charge.  The evaluation program is
  350. fully functional and may be used for up to 30 days.  Usage beyond
  351. that period requires payment of a registration fee.
  352.  
  353.      Development of the RighTime process is ongoing.  We
  354. anticipate reliable accuracies on most hardware to exceed 0.001
  355. second, allowing DOS clock time to be expressed to the millisecond
  356. in a future revision.  An OS/2 version is in planning, with a
  357. summer 1993 anticipated delivery.
  358.